﻿using BarcodeDelay.bean;
using BarcodeDelay.business;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BarcodeDelay.utility
{
    public class Utility
    {
       

        public static string GenerateMD5(string txt)
        {
            using (MD5 mi = MD5.Create())
            {
                byte[] buffer = Encoding.Default.GetBytes(txt);
                //开始加密
                byte[] newBuffer = mi.ComputeHash(buffer);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < newBuffer.Length; i++)
                {
                    sb.Append(newBuffer[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }


        public static void writeTbFile(string fileName,string checkZY)
        {
            try
            {
                string delFile = System.Environment.CurrentDirectory + "\\" + StoParams.Instance.checkTxtPath;
                if(delFile.EndsWith("txt"))
                {
                    File.Delete(delFile);
                    FileStream bFile = new FileStream(delFile, FileMode.Create);
                    StreamWriter swb = null;
                    if (swb == null)
                    {
                        swb = new StreamWriter(bFile);
                    }

                    swb.WriteLine(checkZY);
                    swb.Close();
                }                

            }
            catch (Exception ex)
            {

            }
        }

        public static bool isCompanyDelayedNewGen(RoadInfo roadInfo, Hashtable htFraProvince)
        {
            bool delayed = false;
            try
            {
                /*if (!roadInfo.isSendFromMySite)
                {
                    return delayed;
                }*/
                if (roadInfo.isDepRecvByMeself)
                {
                    return delayed;
                }
                if (isTransferAfterSignNewGen(roadInfo))
                {
                    if (roadInfo.lastSiteName != StoParams.Instance.register_site)
                    {
                        delayed = true;
                        return delayed;
                    }
                    else
                    {
                        delayed = false;
                        return delayed;
                    }
                }
                if(roadInfo.isFakeWeekendDeliver)
                {
                    delayed = true;
                    roadInfo.strErrorMsg = "非周六周日延误送";
                    return delayed;
                }

                if(!roadInfo.isDonePack)
                {
                    if(isLostPackage(roadInfo))
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "漏签或遗失";
                        return delayed;
                    }
                }
                else
                {
                    //判断驿站或者签收时间是否延误
                    delayed = isSignDelay(roadInfo, htFraProvince);
                    return delayed;
                }






            }
            catch(Exception ex)
            {

            }
            return delayed;
        }
        public static bool isLostPackage(RoadInfo roadInfo)
        {
            bool isLostPackage = false;
            try
            {
                if (roadInfo.depMySiteSendTime == "" && roadInfo.lanshouTime != "")
                {
                    roadInfo.depMySiteSendTime = roadInfo.lanshouTime;
                }

                DateTime dtNow = System.DateTime.Now;
                int day = 4;
                if (StoParams.Instance.ht_sto_province_day.ContainsKey(roadInfo.province))
                {
                    RunTimeBean runTimeBean = StoParams.Instance.ht_sto_province_day[roadInfo.province] as RunTimeBean;
                    string strDay = "";
                    strDay = runTimeBean.endDay.ToString();
                    day = Convert.ToInt32(strDay);
                }
                DateTime dtMySiteScanTime = Convert.ToDateTime(roadInfo.depMySiteSendTime);
                DateTime dtEndTime = dtMySiteScanTime.AddDays(day);

                DateTime dtCur = System.DateTime.Now;
                if (dtEndTime > dtCur)
                {
                    isLostPackage = false;
                    return isLostPackage;
                }
                string strEnd = dtEndTime.ToString("yyyy-MM-dd") + " 23:59:59";
                dtEndTime = Convert.ToDateTime(strEnd);
                DateTime dtLastScanTime = Convert.ToDateTime(roadInfo.lastSiteScanDate);
                TimeSpan tsDiff = dtNow - dtLastScanTime;
                if (dtLastScanTime > dtEndTime || tsDiff.TotalHours > 48)
                {
                    isLostPackage = true;
                }
                if (isLostPackage)
                {
                    string strLastScanDate = roadInfo.lastSiteScanDate;
                    TimeSpan ts = new TimeSpan();
                    ts = DateTime.Now - Convert.ToDateTime(strLastScanDate);

                    if (ts.TotalDays >= 3)
                    {
                        roadInfo.strErrorMsg = "遗失";
                    }
                    else
                    {
                        roadInfo.strErrorMsg = "漏签";
                    }

                    bool isRefreshWayBill = isRefreshByMysiteNewGen(roadInfo);
                    if (!isRefreshWayBill)
                    {
                        return isLostPackage;
                    }
                    else
                    {
                        isLostPackage= false;
                    }

                }


            }
            catch(Exception ex)
            {

            }
            return isLostPackage;
        }

        /// <summary>
        /// 是否签收延误
        /// </summary>
        /// <param name="roadInfo"></param>
        /// <returns></returns>
        public static bool isSignDelay(RoadInfo roadInfo,Hashtable htFarProvince)
        {
            bool isSignDelay= false;
            try
            {
                if (roadInfo.depMySiteSendTime == "" && roadInfo.lanshouTime != "")
                {
                    roadInfo.depMySiteSendTime = roadInfo.lanshouTime;
                }

                DateTime dtNow = System.DateTime.Now;
                int day = 4;
                if (StoParams.Instance.ht_sto_province_day.ContainsKey(roadInfo.province))
                {
                    RunTimeBean runTimeBean = StoParams.Instance.ht_sto_province_day[roadInfo.province] as RunTimeBean;
                    string strDay = "";
                    strDay = runTimeBean.endDay.ToString();
                    day = Convert.ToInt32(strDay);
                }
                DateTime dtMySiteScanTime = Convert.ToDateTime(roadInfo.depMySiteSendTime);
                DateTime dtEndTime = dtMySiteScanTime.AddDays(day);

                DateTime dtCur = System.DateTime.Now;
                if (dtEndTime > dtCur)
                {
                    isSignDelay = false;
                    return isSignDelay;
                }
                string strEnd = dtEndTime.ToString("yyyy-MM-dd") + " 23:59:59";
                dtEndTime = Convert.ToDateTime(strEnd);
                DateTime dtLastScanTime = Convert.ToDateTime(roadInfo.lastSiteScanDate);
                TimeSpan tsDiff = dtNow - dtLastScanTime;
                DateTime dtLastSendTime= Convert.ToDateTime(roadInfo.lastSendTime);
                DateTime dtLastArrivaSite = Convert.ToDateTime(roadInfo.lastArrivalSignSiteTime);

                int addDays = 0;

                if(roadInfo.tomorrowSend)
                {
                    addDays++;
                }
                addDays = addDays + roadInfo.addWeekEndDays;
                /*if(dtLastArrivaSite.Hour<12)
                {
                    dtEndTime = Convert.ToDateTime(dtLastArrivaSite.AddDays(addDays).ToShortDateString() + " 23:59:59");
                }
                else
                {
                    addDays++;
                    dtEndTime = Convert.ToDateTime(dtLastArrivaSite.AddDays(addDays).ToShortDateString() + " 23:59:59");
                }*/
                DateTime dtDoneTime = dtLastScanTime;
                if (roadInfo.yizhanRecTime!="")
                {
                    dtDoneTime = Convert.ToDateTime(roadInfo.yizhanRecTime);
                }
                else
                {
                    dtDoneTime = Convert.ToDateTime(roadInfo.donePackTime);
                }
                if(roadInfo.actConculTime=="")
                {
                    handleActConculTime(roadInfo);
                }
                if(roadInfo.actConculTime!="")
                {
                    dtEndTime = Convert.ToDateTime(roadInfo.actConculTime).AddDays(addDays);
                }
                else
                {
                    if (dtLastArrivaSite.Hour < 12)
                    {
                        dtEndTime = Convert.ToDateTime(dtLastArrivaSite.AddDays(addDays).ToShortDateString() + " 23:59:59");
                    }
                    else
                    {
                        addDays++;
                        dtEndTime = Convert.ToDateTime(dtLastArrivaSite.AddDays(addDays).ToShortDateString() + " 23:59:59");
                    }

                    if (dtDoneTime > dtEndTime)
                    {
                        isSignDelay = true;
                        roadInfo.strErrorMsg = "签收延误";
                    }

                    addDays = 0;
                    if (!isSignDelay)
                    {
                        if (htFarProvince.ContainsKey(roadInfo.province))
                        {
                            addDays++;
                        }
                        if (roadInfo.tomorrowSend)
                        {
                            addDays++;
                        }
                        addDays = addDays + roadInfo.addWeekEndDays;
                        if (dtLastSendTime.Hour < 6)
                        {
                            dtEndTime = Convert.ToDateTime(dtLastSendTime.AddDays(addDays).ToShortDateString() + " 23:59:59");
                        }
                        else
                        {
                            addDays++;
                            dtEndTime = Convert.ToDateTime(dtLastSendTime.AddDays(addDays).ToShortDateString() + " 23:59:59");
                        }
                        if (dtDoneTime > dtEndTime)
                        {
                            isSignDelay = true;
                            roadInfo.strErrorMsg = "签收延误";
                        }
                    }
                }
                if(dtDoneTime> dtEndTime)
                {
                    isSignDelay = true;
                    roadInfo.strErrorMsg = "签收延误";
                }
                /*addDays = 0;
                if(!isSignDelay)
                {
                    if (htFarProvince.ContainsKey(roadInfo.province))
                    {
                        addDays++;
                    }
                    if (roadInfo.tomorrowSend)
                    {
                        addDays++;
                    }
                    if (dtLastSendTime.Hour < 6)
                    {
                        dtEndTime = Convert.ToDateTime(dtLastSendTime.AddDays(addDays).ToShortDateString() + " 23:59:59");
                    }
                    else
                    {
                        addDays++;
                        dtEndTime = Convert.ToDateTime(dtLastSendTime.AddDays(addDays).ToShortDateString() + " 23:59:59");
                    }
                    if (dtDoneTime > dtEndTime)
                    {
                        isSignDelay = true;
                        roadInfo.strErrorMsg = "签收延误";
                    }
                }*/




            }
            catch (Exception ex)
            {

            }
            return isSignDelay;
        }
        /*public static bool isLostPackage(RoadInfo roadInfo)
        {
            bool isLostPackage = false;
            string strRunTime = roadInfo.runTime;
            DateTime dtNow = System.DateTime.Now;
            DateTime dtLastScanTime = Convert.ToDateTime(roadInfo.lastSiteScanDate);
            TimeSpan tsDiff = dtNow - dtLastScanTime;

            if (dtLastScanTime > dtEndTime || tsDiff.TotalHours > 48)
            {
                delayed = true;
            }
            if (delayed)
            {
                if (strRunTime.Contains("天"))
                {
                    string strCostDay = strRunTime.Substring(0, strRunTime.IndexOf("天"));
                    string strLastScanDate = roadInfo.lastSiteScanDate;
                    TimeSpan ts = new TimeSpan();
                    if (strLastScanDate != "")
                    {
                        ts = DateTime.Now - Convert.ToDateTime(strLastScanDate);
                    }

                    if (ts.TotalDays >= 3)
                    {
                        roadInfo.strErrorMsg = "遗失";
                    }
                    else
                    {
                        roadInfo.strErrorMsg = "漏签";
                    }

                }
                else
                {
                    roadInfo.strErrorMsg = "漏签";
                }

                bool isRefreshWayBill = isRefreshByMysite(roadInfo);
                if (!isRefreshWayBill)
                {
                    return delayed;
                }
                else
                {
                    return false;
                }


            }
        }*/


        public static bool isCompanyDelayed(RoadInfo roadInfo,Hashtable htFraProvince)
        {
            bool delayed = false;
            try
            {
         
                if (!roadInfo.isSendFromMySite)
                {
                    return delayed;
                }
                if(roadInfo.isReturnPackage)
                {
                    delayed = isReturnPackageDelayed(roadInfo);
                    if(delayed)
                    {
                        roadInfo.strErrorMsg = "退回件延误";
                        return delayed;
                    }
                   
                }
                if (roadInfo.isDepRecvByMeself)
                {
                    return delayed;
                }
                if (roadInfo.isGetOutByDeliver)
                {
                    delayed = isDeliverMultiOutAndIn(roadInfo);
                    if(delayed)
                    {
                        return delayed;
                    }                    
                }
                if(isTransferAfterSign(roadInfo))
                {
                    if(roadInfo.lastSiteName!=StoParams.Instance.register_site)
                    {
                        delayed = true;
                        return delayed;
                    }
                    else
                    {
                        delayed = false;
                        return delayed;
                    }
                    
                }
                if (roadInfo.errorScanTimes > 0)
                {
                    bool isChaoPai = false;
                    bool isDoubleTomorrow = false;
                    DeliverdByTomorrow(roadInfo, out isChaoPai, out isDoubleTomorrow);
                    //以下测试代码
                    /*if (roadInfo.tomorrowSend)
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "明日送";
                        return delayed;
                    }*/


                    if (isChaoPai || isDoubleTomorrow)
                    {
                        delayed = false;
                        return delayed;
                    }

                    if (!roadInfo.isSortWrong&&!roadInfo.tomorrowSend)
                    {
                        bool isDeliverUseWeekend = false;
                        bool isWeekEnd = isDeliverFailedByWeekend(roadInfo,out isDeliverUseWeekend);
                        if (isWeekEnd)
                        {
                            delayed = false;
                            TimeSpan ts = new TimeSpan();
                            bool onTimeArrival = isPackagDelay(roadInfo, out ts);
                            if(onTimeArrival&&ts.TotalHours>48)
                            {
                                delayed = true;
                                roadInfo.strErrorMsg = "到件延误";

                            }                             
                            else
                            {
                                roadInfo.strErrorMsg = "";
                            }
                            
                            return delayed;
                        }
                        else
                        {
                            if(isDeliverUseWeekend)
                            {
                                delayed = true;
                                roadInfo.strErrorMsg = "非节假日延误";
                                return delayed;
                            }
                        }
                     
                    }
                    else
                    {                       
                        isTimeDealyedByWrongSort(roadInfo);
                    }                   

                }

                if (roadInfo.depMySiteSendTime == "" && roadInfo.lanshouTime != "")
                {
                    roadInfo.depMySiteSendTime = roadInfo.lanshouTime;
                }

                DateTime dtNow = System.DateTime.Now;
                int day = 4;
                if (StoParams.Instance.ht_sto_province_day.ContainsKey(roadInfo.province))
                {
                    RunTimeBean runTimeBean = StoParams.Instance.ht_sto_province_day[roadInfo.province] as RunTimeBean;
                    string strDay = "";
                    if ((roadInfo.lastSiteStatus.Contains("派送") || roadInfo.lastSiteStatus.Contains("签收") || roadInfo.assignPackTime != ""))
                    {
                        strDay = runTimeBean.endDay.ToString();
                    }
                    else
                    {
                        if (roadInfo.yizhanRecTime == "")
                        {
                            strDay = runTimeBean.beginDay.ToString();
                        }
                        else
                        {
                            strDay = runTimeBean.endDay.ToString();
                        }
                    }

                    day = Convert.ToInt32(strDay);
                }
                else
                {
                    if (roadInfo.province.Length > 0)
                    {
                        int a = 0;
                        //swb.WriteLine(roadInfo.province + "不存在");
                    }

                }
                DateTime dtMySiteScanTime = Convert.ToDateTime(roadInfo.depMySiteSendTime);
                DateTime dtEndTime = dtMySiteScanTime.AddDays(day);

                DateTime dtCur = System.DateTime.Now;
                if(dtEndTime>dtCur)
                {
                    delayed = false;
                    return delayed;
                }
                string strEnd = dtEndTime.ToString("yyyy-MM-dd") + " 23:59:59";
                dtEndTime = Convert.ToDateTime(strEnd);
                if (roadInfo.isErrorPackage)
                {
                    DateTime dtErrorScan = Convert.ToDateTime(roadInfo.errorScanTime);
                    if (dtErrorScan > dtEndTime)
                    {
                        delayed = true;
                        if(roadInfo.isSortWrong)
                        {
                            roadInfo.strErrorMsg = "错发延误";
                            return delayed;
                        }
                    }
                    if(roadInfo.tomorrowSend)
                    {
                        dtEndTime = dtEndTime.AddDays(1);
                    }
                }


                //先判断到达驿站或者签收的时间是否超时
                if (roadInfo.yizhanRecTime != "" || roadInfo.donePackTime != "")
                {
                    if (roadInfo.yizhanRecTime != "")
                    {
                        if (roadInfo.depMySiteSendTime != "")
                        {

                            if (Convert.ToDateTime(roadInfo.yizhanRecTime) > dtEndTime)
                            {
                                delayed = true;
                            }
                            if (delayed)
                            {
                                roadInfo.strErrorMsg = "延误";
                                if(roadInfo.isSortWrong)
                                {
                                    roadInfo.strErrorMsg = "错发延误";
                                }
                            }
                        }

                    }
                    else if (roadInfo.donePackTime != "")
                    {
                        DateTime dtDonePackTime = Convert.ToDateTime(roadInfo.donePackTime);
                        if (dtDonePackTime > dtEndTime)
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "延误";
                            if(roadInfo.isSortWrong)
                            {
                                roadInfo.strErrorMsg = "错发延误";
                            }
                        }
                    }

                }
                //没有签收，漏签
                else
                {
                    //只需要判断runtime是否大于指定的天数
                    string strRunTime = roadInfo.runTime;
                    DateTime dtLastScanTime = Convert.ToDateTime(roadInfo.lastSiteScanDate);
                    TimeSpan tsDiff = dtNow - dtLastScanTime;
                    if (dtLastScanTime > dtEndTime|| tsDiff.TotalHours>48)
                    {
                        delayed = true;
                    }
                    if (delayed)
                    {
                        if (strRunTime.Contains("天"))
                        {
                            string strCostDay = strRunTime.Substring(0, strRunTime.IndexOf("天"));
                            string strLastScanDate = roadInfo.lastSiteScanDate;
                            TimeSpan ts = new TimeSpan();
                            if(strLastScanDate!="")
                            {
                                ts = DateTime.Now - Convert.ToDateTime(strLastScanDate);
                            }

                            if (ts.TotalDays >= 3)
                            {
                                roadInfo.strErrorMsg = "遗失";
                            }
                            else
                            {
                                roadInfo.strErrorMsg = "漏签";
                            }

                        }
                        else
                        {
                            roadInfo.strErrorMsg = "漏签";
                        }

                        bool isRefreshWayBill = isRefreshByMysite(roadInfo);
                        if(!isRefreshWayBill)
                        {
                            return delayed;
                        }
                        else
                        {
                            return false;
                        }
                       

                    }

                }
                int addDelayDays = 0;
                if(htFraProvince.ContainsKey(roadInfo.province))
                {
                    addDelayDays = 1;
                }

                //先通过通用的方法判断延误，再判断是否是网点延误
                if (delayed&&!roadInfo.strErrorMsg.Contains("遗失")&&!roadInfo.isSortWrong)
                {
                    delayed = false;
                    bool isHaveAssinSacn = false;
                    string strAssinSite = "";
                    int assinIndex = 0;
                    int curIndex = 0;
                    string strFirstCompany = "";
                    int ZYToCompanyIndex = 0;
                    string zyToCompanyTime = "";
                    MemInfo memFirstCompany = null;
                    MemInfo memAssignPack = null;
                    int lastSignIndex = 0;
                    int firstAssingIndex = 0;

                    int firstZyIndex = 0;
                    string strFirstZYCenter = "";
                    int doneIndex = 0;
                    string priorSacnType="";
                    DateTime dtCenterToCompany=Convert.ToDateTime("2000-01-01 00:00:00");
                    string strCenterName = "";
                    bool isTransferByCenter = false;
                    MemInfo priorMemInfo = new MemInfo();
                    MemInfo firstAssignMemInfo = new MemInfo();
                    string dateTimeLastQianshou = "";
                    string strSendToCompany = "";
                 
                    foreach (MemInfo memInfo in roadInfo.arListMems)
                    {
                        curIndex++;
                        string strScanType = memInfo.scanType;
                        string strMem = memInfo.memMsg;
                        string strPriNext = memInfo.preOrNext;
                        string strSacnDate = memInfo.scanTime;
                        string strUploadStie = memInfo.uploadSiteName;
                        if (memInfo.uploadSiteName.Contains("集散") || memInfo.uploadSiteName.Contains("转运中心") || memInfo.uploadSiteName.Contains("分拨")
                                            || memInfo.uploadSiteName.Contains("航空部"))
                        {
                            isTransferByCenter = true;
                        }
                        if (strScanType== "已签收")
                        {
                            if(priorSacnType=="运输中"|| priorSacnType == "派送中")
                            {
                                if(!isYiZhanRec(priorMemInfo.memMsg, priorMemInfo))
                                {
                                    delayed = true;
                                    roadInfo.strErrorMsg = "签收后延误";
                                    roadInfo.lastSiteName = strUploadStie;
                                    return delayed;
                                }
                                
                            }

                        }
                        priorSacnType = strScanType;
                        priorMemInfo = memInfo;
                        if (memInfo.scanType=="派送中"&& firstAssingIndex==0&& curIndex < roadInfo.arListMems.Count / 3)
                        {
                            firstAssingIndex = curIndex;
                            firstAssignMemInfo = memInfo;
                        }
                        if(memInfo.scanType=="已签收"&& curIndex < roadInfo.arListMems.Count / 2)
                        {
                            lastSignIndex = curIndex;
                            dateTimeLastQianshou = strSacnDate;
                        }
                        
                        if (memInfo.memMsg.EndsWith("派件中") && curIndex < roadInfo.arListMems.Count / 3)
                        {
                            isHaveAssinSacn = true;
                            strAssinSite = strUploadStie;
                            assinIndex = curIndex;
                            memAssignPack = memInfo;
                        }
                        if(strFirstCompany==""&& curIndex< roadInfo.arListMems.Count)
                        {
                            if(isCurSiteIsZYToCompany(memInfo))
                            {
                                roadInfo.lastCompanyName = strPriNext;
                                strFirstCompany = strPriNext;
                                ZYToCompanyIndex = curIndex;
                                int firstCompanyIdex = curIndex;
                                if (firstCompanyIdex >= 2)
                                {
                                    while(firstCompanyIdex >= 2)
                                    {
                                        memFirstCompany = roadInfo.arListMems[firstCompanyIdex - 2] as MemInfo;
                                        if(memFirstCompany.uploadSiteName.Contains("集散")|| memFirstCompany.uploadSiteName.Contains("转运中心") || memFirstCompany.uploadSiteName.Contains("分拨")
                                            || memFirstCompany.uploadSiteName.Contains("航空部"))
                                        {
                                            firstCompanyIdex--;
                                        }
                                        else
                                        {
                                            ZYToCompanyIndex = firstCompanyIdex;
                                            break;
                                        }
                                    }                                   

                                }

                            }//if (isCurSiteIsZYToCompany(memInfo))
                        }
                        if(strFirstZYCenter==""&& (memInfo.uploadSiteName.Contains("转运中心")|| memInfo.uploadSiteName.Contains("集散")) && curIndex < roadInfo.arListMems.Count)
                        {
                            strFirstZYCenter = memInfo.uploadSiteName;
                            firstZyIndex = curIndex - 1;
                            dtCenterToCompany = Convert.ToDateTime(memInfo.scanTime);
                            strCenterName = memInfo.uploadSiteName;
                        }                      

                    }

                    foreach (MemInfo memInfo in roadInfo.arListMems)
                    {
                        if (isCurSiteIsZYToCompany(memInfo))
                        {
                            if(memInfo.uploadSiteName== strCenterName&& strFirstCompany== memInfo.preOrNext)
                            {
                                if(Convert.ToDateTime(memInfo.scanTime) < dtCenterToCompany)
                                {
                                    dtCenterToCompany = Convert.ToDateTime(memInfo.scanTime);
                                    break;
                                }
                            }
                        }
                    }

                    if (memFirstCompany==null&& firstZyIndex>=1)
                    {
                        memFirstCompany = roadInfo.arListMems[firstZyIndex-1] as MemInfo;
                        if(ZYToCompanyIndex==0)
                        {
                            ZYToCompanyIndex = firstZyIndex;
                        }
                    }


                    if(lastSignIndex >firstAssingIndex&& firstAssingIndex>0)
                    {
                        string strFirstAssignDate = firstAssignMemInfo.scanTime;
                        if(strFirstAssignDate!=""&&dateTimeLastQianshou!="")
                        {
                            DateTime dtFirstAssi = Convert.ToDateTime(strFirstAssignDate.Substring(0, strFirstAssignDate.IndexOf(' ')));
                            DateTime dtLastAssi = Convert.ToDateTime(strFirstAssignDate.Substring(0, dateTimeLastQianshou.IndexOf(' ')));
                            if(dtFirstAssi>dtLastAssi)
                            {
                                roadInfo.strErrorMsg = "签收后延误";
                                delayed = true;
                                return delayed;
                            }
                        }
                        
                    }
                   

                    bool isHaveArrivae = false;
                    string strArriveCompany = "";
                    for(int i= ZYToCompanyIndex-1;i>=0;i--)
                    {
                        MemInfo memInfo = roadInfo.arListMems[i] as MemInfo;
                        if(memInfo.memMsg.Contains("已到达-")&& !memInfo.memMsg.Contains("集散")&& !memInfo.memMsg.Contains("转运中心") && !memInfo.memMsg.Contains("航空部"))
                        {
                            strArriveCompany = memInfo.scanTime;
                            isHaveArrivae = true;
                            break;
                        }
                    }


                    bool isCustomerSign = false;
                    if(roadInfo.yizhanRecTime!=""||roadInfo.donePackTime!="")
                    {
                        isCustomerSign = true;
                    }

                    /*if (!isHaveArrivae&& isTransferByCenter)
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "未做到件";
                    }*/
                    /*else if (!isHaveAssinSacn)
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "未做派件";
                    }*/
                    else if (!isCustomerSign)
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "客户未签收";
                    }
                    //到件后 派件延误
                    if(memFirstCompany!=null&&memAssignPack!=null)
                    {
                        string strFirstCompanyScanDate = memFirstCompany.scanTime;
                        string strAssignDate = memAssignPack.scanTime;
                        if(strFirstCompanyScanDate!=""&& strFirstCompanyScanDate.Length>5&& strAssignDate!=""&& strAssignDate.Length>5)
                        {

                            DateTime dtFirstScan = Convert.ToDateTime(strFirstCompanyScanDate);
                            if(roadInfo.isFarCompany&& roadInfo.farCompanyScanTime!="")
                            {
                                DateTime dtFarCompanySacn = Convert.ToDateTime(roadInfo.farCompanyScanTime);
                                if(dtFarCompanySacn> dtFirstScan)
                                {
                                    strFirstCompanyScanDate = dtFarCompanySacn.AddDays(roadInfo.farCompanyDelay).ToString();
                                }
                            }
                            dtFirstScan = Convert.ToDateTime(strFirstCompanyScanDate);
                            int delayArrival = 0;
                            if(dtFirstScan.Hour>18)
                            {
                                delayArrival = 1; 
                            }
                            if (roadInfo.tomorrowSend)
                            {
                                delayArrival = delayArrival+1;
                            }
                            DateTime dtFirst = Convert.ToDateTime(strFirstCompanyScanDate.Substring(0, strFirstCompanyScanDate.IndexOf(' '))).AddDays(addDelayDays);
                            dtFirst = dtFirst.AddDays(delayArrival);
                            DateTime dtAssign= Convert.ToDateTime(strAssignDate.Substring(0,strAssignDate.IndexOf(' ')));

                            if(dtAssign> dtFirst)
                            {
                                if(!roadInfo.isErrorPackage)
                                {
                                    delayed = true;
                                    roadInfo.strErrorMsg = "到件后派件延误";
                                }
                                else
                                {
                                    if(roadInfo.errorScanTime!="")
                                    {
                                        DateTime dtErrorSacn = Convert.ToDateTime(roadInfo.errorScanTime);
                                        dtErrorSacn = Convert.ToDateTime(dtErrorSacn.ToShortDateString());
                                        if (dtErrorSacn > dtFirst&& roadInfo.errorScanTimes==1)
                                        {
                                            delayed = true;
                                            roadInfo.strErrorMsg = "问题件延误";
                                            
                                        }
                                    }
                                    
                                }
                                
                            }
                        }

                    }
                    //派件后签收延误
                    if (memAssignPack != null&& isCustomerSign)
                    {
                        string strAssignDate = memAssignPack.scanTime;
                        string strSignTime = roadInfo.yizhanRecTime;
                        if(strSignTime==""|| strSignTime.Length<5)
                        {
                            strSignTime = roadInfo.donePackTime;
                        }
                        if (strAssignDate != "" && strAssignDate.Length > 5)
                        {
                            DateTime dtDone = Convert.ToDateTime(strSignTime.Substring(0, strSignTime.IndexOf(' ')));
                            DateTime dtAssign = Convert.ToDateTime(strAssignDate.Substring(0, strAssignDate.IndexOf(' '))).AddDays(addDelayDays);
                            
                            if (dtDone> dtAssign)
                            {
                                if (!roadInfo.isErrorPackage)
                                {
                                    delayed = true;
                                    roadInfo.strErrorMsg = "派件后签收延误";
                                }
                                else
                                {
                                    if(roadInfo.errorScanTime!="")
                                    {
                                        DateTime dtErrorSacn = Convert.ToDateTime(roadInfo.errorScanTime);
                                        if (dtErrorSacn > dtEndTime)
                                        {
                                            delayed = true;
                                            roadInfo.strErrorMsg = "问题件延误";
                                        }
                                    }
                                    
                                }
                                
                            }
                        }

                    }
                    if(!delayed)
                    {
                        string strQianshouTime = "";
                        if(roadInfo.yizhanRecTime!="")
                        {
                            strQianshouTime = roadInfo.yizhanRecTime;
                        }
                        else
                        {
                            strQianshouTime = roadInfo.donePackTime;
                        }
                        string strCenterSendTime = getShouldEndDateTime(dtCenterToCompany, strCenterName,roadInfo.lastCompanyName,roadInfo);
                       
                        string strDeadTime = strArriveCompany;
                        if(strCenterSendTime != "")
                        {
                            strDeadTime = strCenterSendTime;
                        }
            
                        if (strQianshouTime!=""&& strDeadTime != "")
                        {
                            DateTime dtQiasnshou = Convert.ToDateTime(strQianshouTime.Substring(0, strQianshouTime.IndexOf(' ')));
                            DateTime dtDeadTime = Convert.ToDateTime(strDeadTime.Substring(0, strDeadTime.IndexOf(' '))).AddDays(addDelayDays+roadInfo.farCompanyDelay);
                            if(dtQiasnshou> dtDeadTime && roadInfo.errorScanTime == "")
                            {
                                delayed = true;
                                roadInfo.strErrorMsg = "签收超时";
                            }
                        }

                    }

                    if (roadInfo.isGetOutByDeliver&&!delayed)
                    {
                        delayed = isDeliverMultiOutAndIn(roadInfo);
                        if (delayed)
                        {
                            return delayed;
                        }

                    }

                }
                else
                {
                    if(roadInfo.isSortWrong&&!delayed)
                    {
                        string strFirstSendToCompany = roadInfo.firstSendToCompany;
                        string centerName = roadInfo.lastCenterName;
                        string strQianshouTime = "";
                        if (roadInfo.yizhanRecTime != "")
                        {
                            strQianshouTime = roadInfo.yizhanRecTime;
                        }
                        else
                        {
                            strQianshouTime = roadInfo.donePackTime;
                        }
                        string strCenterSendTime = getShouldEndDateTime(Convert.ToDateTime(strFirstSendToCompany), centerName,roadInfo.lastCompanyName,roadInfo);
                        string strDeadTime = "";
                        if (strCenterSendTime != "")
                        {
                            strDeadTime = strCenterSendTime;
                        }
                        if (strQianshouTime != "" && strDeadTime != "")
                        {
                            DateTime dtQiasnshou = Convert.ToDateTime(strQianshouTime.Substring(0, strQianshouTime.IndexOf(' ')));
                            DateTime dtDeadTime = Convert.ToDateTime(strDeadTime.Substring(0, strDeadTime.IndexOf(' '))).AddDays(addDelayDays+roadInfo.farCompanyDelay);
                            if (dtQiasnshou > dtDeadTime && roadInfo.errorScanTime == "")
                            {
                                delayed = true;
                                roadInfo.strErrorMsg = "签收超时";
                            }
                        }
                    }
                    else
                    {
                        if(!delayed&&!roadInfo.isErrorPackage)
                        {
                            string strQianshouTime = "";
                            if (roadInfo.yizhanRecTime != "")
                            {
                                strQianshouTime = roadInfo.yizhanRecTime;
                            }
                            else
                            {
                                strQianshouTime = roadInfo.donePackTime;
                            }
                            if(roadInfo.assignPackTime!=""&& strQianshouTime!="")
                            {
                                DateTime dtQianshou = Convert.ToDateTime(strQianshouTime.Substring(0, strQianshouTime.IndexOf(' ')));
                                DateTime dtAssign = Convert.ToDateTime(roadInfo.assignPackTime.Substring(0, roadInfo.assignPackTime.IndexOf(' ')));
                                if(dtQianshou>dtAssign)
                                {
                                    delayed = true;
                                    roadInfo.strErrorMsg = "签收超时";
                                }
                            }
                            if(!delayed && !roadInfo.isErrorPackage)
                            {
                                int delayDay = 0;
                                if(htFraProvince.ContainsKey(roadInfo.province))
                                {
                                    delayDay++;
                                }
                                if(roadInfo.isFarCompany)
                                {
                                    delayDay++;
                                }

                                DateTime dtOrArrival = Convert.ToDateTime(Convert.ToDateTime(roadInfo.arriveSiteTime).ToString("yyyy-MM-dd HH:mm"));
                                DateTime dtAssign = Convert.ToDateTime(roadInfo.assignPackTime.Substring(0, roadInfo.assignPackTime.IndexOf(' ')));
                                DateTime dtArrivalSite= Convert.ToDateTime(roadInfo.arriveSiteTime.Substring(0, roadInfo.arriveSiteTime.IndexOf(' ')));
                                dtArrivalSite = dtArrivalSite.AddDays(delayDay);
                                if(dtAssign> dtArrivalSite)
                                {
                                    TimeSpan ts = dtAssign - dtArrivalSite;
                                    if(ts.TotalDays>1|| dtOrArrival.Hour<=15)
                                    {
                                        delayed = true;
                                        roadInfo.strErrorMsg = "派送超时";
                                    }
                                }
                            }//判断到达时间，与派送时间差
                        }
                    }
                }               

            }
            catch(Exception ex)
            {

            }
            return delayed;
        }

        public static bool isZYCenterDelayed(RoadInfo roadInfo,Hashtable htFarProvince)
        {
            bool delayed = false;
            try
            {
                if(isCompanyDelayed(roadInfo, htFarProvince))
                {
                    delayed = true;
                    return delayed;
                }
                if (!roadInfo.isSendFromMySite)
                {
                    return delayed;
                }
                if (roadInfo.isDepRecvByMeself)
                {
                    return delayed;
                }

                if (roadInfo.isGetOutByDeliver)
                {
                    delayed = isDeliverMultiOutAndIn(roadInfo);
                    if (delayed)
                    {
                        return delayed;
                    }
                }
                if (roadInfo.errorScanTimes>0)
                {
                    bool isChaoPai = false;
                    bool isDoubleTomorrow = false;
                    bool isDeliverUseWeekend = false;
                    bool isWeekEnd = isDeliverFailedByWeekend(roadInfo,out isDeliverUseWeekend);
                    if(isWeekEnd)
                    {
                        delayed = false;
                        roadInfo.strErrorMsg = "";
                        return delayed;
                    }
                    else
                    {
                        DeliverdByTomorrow(roadInfo,out isChaoPai,out isDoubleTomorrow);
                        if(isChaoPai|| isDoubleTomorrow)
                        {
                            delayed = false;
                            roadInfo.strErrorMsg = "";
                            return delayed;
                        }
                    }
                }

                if (roadInfo.depMySiteSendTime == "" && roadInfo.lanshouTime != "")
                {
                    roadInfo.depMySiteSendTime = roadInfo.lanshouTime;
                }

                DateTime dtNow = System.DateTime.Now;
                int day = 4;
                if (StoParams.Instance.ht_sto_province_day.ContainsKey(roadInfo.province))
                {
                    RunTimeBean runTimeBean = StoParams.Instance.ht_sto_province_day[roadInfo.province] as RunTimeBean;
                    string strDay = "";
                    if ((roadInfo.lastSiteStatus.Contains("派送") || roadInfo.lastSiteStatus.Contains("签收") || roadInfo.assignPackTime != ""))
                    {
                        strDay = runTimeBean.endDay.ToString();

                    }
                    else
                    {
                        if (roadInfo.yizhanRecTime == "")
                        {
                            strDay = runTimeBean.beginDay.ToString();
                        }
                        else
                        {
                            strDay = runTimeBean.endDay.ToString();
                        }

                    }

                    day = Convert.ToInt32(strDay);
                }
                else
                {
                    if (roadInfo.province.Length > 0)
                    {
                        int a = 0;
                        //swb.WriteLine(roadInfo.province + "不存在");
                    }

                }
                DateTime dtMySiteScanTime = Convert.ToDateTime(roadInfo.depMySiteSendTime);
                DateTime dtEndTime = dtMySiteScanTime.AddDays(day);
                string strEnd = dtEndTime.ToString("yyyy-MM-dd") + " 23:59:59";
                dtEndTime = Convert.ToDateTime(strEnd);
                if (roadInfo.isErrorPackage)
                {
                    DateTime dtErrorScan = Convert.ToDateTime(roadInfo.errorScanTime);
                    if (dtErrorScan > dtEndTime)
                    {
                        delayed = true;
                    }
                    else
                    {
                       
                        if (dtErrorScan.Day == dtEndTime.Day || dtErrorScan.Day == dtEndTime.Day - 1)
                        {
                            return delayed;
                        }
                        if (roadInfo.tomorrowSend)
                        {
                            dtEndTime=dtEndTime.AddDays(1);
                        }
                    }

                }
                if (roadInfo.lastSiteName == StoParams.Instance.register_site)
                {
                    DateTime dtLastSacnTime = Convert.ToDateTime(roadInfo.depMySiteSendTime);
                    TimeSpan ts = dtNow - dtLastSacnTime;
                    if (ts.TotalDays >= 2&&!roadInfo.isDelverByZy)
                    {
                        roadInfo.errorType = 8;
                        roadInfo.strErrorMsg = "有发件无到件";
                        delayed = true;
                    }
                }
                else
                {
                    //先判断到达驿站或者签收的时间是否超时
                    if (roadInfo.yizhanRecTime != "" || roadInfo.donePackTime != "")
                    {
                        if (roadInfo.yizhanRecTime != "")
                        {
                            if (roadInfo.depMySiteSendTime != "")
                            {

                                if (Convert.ToDateTime(roadInfo.yizhanRecTime) > dtEndTime)
                                {
                                    delayed = true;
                                }
                                if (delayed)
                                {
                                    roadInfo.strErrorMsg = "延误";
                                }
                            }
                            /*if (!delayed)
                            {
                                //判断驿站时间是否超时以及7天未取
                                DateTime dtFinsh = System.DateTime.Now;
                                if (roadInfo.donePackTime != "")
                                {
                                    dtFinsh = Convert.ToDateTime(roadInfo.donePackTime);
                                }
                                TimeSpan ts = dtFinsh - Convert.ToDateTime(roadInfo.yizhanRecTime);
                                if (ts.TotalDays > 7)
                                {
                                    delayed = true;
                                    roadInfo.strErrorMsg = "驿站存放时间" + ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes + "分钟" + " 超过7天";
                                    roadInfo.errorType = 5;
                                }
                            }*/

                        }
                        else if (roadInfo.donePackTime != "")
                        {
                            DateTime dtDonePackTime = Convert.ToDateTime(roadInfo.donePackTime);
                            if (dtDonePackTime > dtEndTime)
                            {
                                delayed = true;
                                roadInfo.strErrorMsg = "延误";
                            }
                        }

                    }

                    else
                    {
                        //只需要判断runtime是否大于指定的天数
                        string strRunTime = roadInfo.runTime;
                        DateTime dtLastScanTime = Convert.ToDateTime(roadInfo.lastSiteScanDate);
                        TimeSpan tsDiff = dtNow - dtLastScanTime;
                        if (dtLastScanTime > dtEndTime || tsDiff.TotalHours > 48)
                        {
                            delayed = true;
                        }



                        //delayed =isDepTimeExpire(roadInfo.province, ts.ToString(), roadInfo.lastScanStatus);
                        if (delayed)
                        {
                            if (strRunTime.Contains("天"))
                            {
                                string strCostDay = strRunTime.Substring(0, strRunTime.IndexOf("天"));
                                if (Convert.ToInt32(strCostDay) >= 3)
                                {
                                    roadInfo.strErrorMsg = "延误或遗失";
                                }
                                else
                                {
                                    roadInfo.strErrorMsg = "延误";
                                }
                            }
                            else
                            {
                                roadInfo.strErrorMsg = "延误";
                            }

                        }

                    }

                }
            }
            catch (Exception ex)
            {

            }
            return delayed;
        }

        public static bool isCurSiteIsZYToCompany(MemInfo memInfo)
        {
            bool res = false;
            try
            {
                if(memInfo.uploadSiteName.Contains("转运中心")|| memInfo.uploadSiteName.Contains("集散")|| memInfo.uploadSiteName.Contains("航空部"))
                {
                    string strMemMsg = memInfo.memMsg;
                    strMemMsg = strMemMsg.Replace("】", "");
                    if(strMemMsg.EndsWith("公司")|| strMemMsg.EndsWith("营业部") || strMemMsg.EndsWith("承包区") || (strMemMsg.EndsWith("部")&& !strMemMsg.Contains("航空")))
                    {
                        res = true;
                    }
                }
            }
            catch(Exception ex)
            {

            }
            return res;
        }

        private static bool isYiZhanRec(string Memo,MemInfo memInfo)
        {
            bool res = false;
            try
            {
                if(Memo.EndsWith("-派件中")|| Memo.EndsWith("-派送中"))
                {
                    return res;
                }
                if (Utility.isPackToYizhan(Memo, memInfo.scanType))
                {
                    res = true;
                }
            }
            catch(Exception ex)
            {

            }
            return res;
        }

        public static bool isPackToYizhan(string Memo,string strType)
        {
            bool res = false;
            try
            {
                if ((Memo.Contains("丰巢智能柜") || Memo.Contains("代收，")||Memo.Contains("代收。")||Memo.Contains("代收,") ||Memo.Contains("摩西管家")
                   || Memo.Contains("驿站") || Memo.Contains("自提柜") || Memo.Contains("代保管")
                   ||(Memo.Contains("凭取件码")&&Memo.Contains("至")&&Memo.Contains("取尾号"))
                   ) && !Memo.Contains("已签收,") && strType != "已签收")
                {
                    res = true;
                }
            }
            catch(Exception ex)
            {

            }
            return res;
        }
        public static bool isChaopaiDelayedNewGen(RoadInfo roadInfo)
        {
            bool isChaoPaiDelay = false;
            try
            {

            }
            catch(Exception ex)
            {

            }
            return isChaoPaiDelay;

        }
        /// <summary>
        /// 超派件延误
        /// </summary>
        /// <param name="roadInfo"></param>
        /// <returns></returns>
        public static bool isChaopaiDelayed(RoadInfo roadInfo)
        {
            bool isChaoPaiDelay = false;
            try
            {

                string strErrorScanTime = roadInfo.errorScanTime;
                DateTime dtChaopai= System.DateTime.Now;
                DateTime dtDoneTime = System.DateTime.Now;
                bool isFindReturnPackage = false;
                int returnIndex = 0;
                int curIndex = 0;
                bool isPackDone = false;
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    curIndex++;
                    string Memo = memInfo.memMsg;
                    
                    if (memInfo.memMsg.Contains("超派件") && curIndex <= 4)
                    {
                        dtChaopai = Convert.ToDateTime(memInfo.scanTime);            
       
                    }

                }
                if(roadInfo.yizhanRecTime!="")
                {
                    isPackDone = true;
                    dtDoneTime = Convert.ToDateTime(roadInfo.yizhanRecTime);
                }
                else if(roadInfo.donePackTime!="")
                {
                    isPackDone = true;
                    dtDoneTime = Convert.ToDateTime(roadInfo.donePackTime);
                }
                if(isPackDone)
                {
                    DateTime dtShouldDone = Convert.ToDateTime(dtChaopai.AddDays(1).ToShortDateString() + " 23:59:59");
                    if(dtDoneTime> dtShouldDone)
                    {
                        isChaoPaiDelay = true;
                        return isChaoPaiDelay;
                    }
                }
                else
                {
                    DateTime dtShouldDone = Convert.ToDateTime(dtChaopai.ToShortDateString() + " 23:59:59");
                    if(DateTime.Now> dtShouldDone)
                    {
                        isChaoPaiDelay = true;
                        return isChaoPaiDelay;
                    }
                }


            }
            catch (Exception ex)
            {

            }
            return isChaoPaiDelay;

        }
        public static bool isReturnPackageDelayedNewGen(RoadInfo roadInfo)
        {
            bool isReturnPackageDelay = false;
            try
            {
                string strErrorScanTime = roadInfo.errorScanTime;
                DateTime dtReturnDateTime = System.DateTime.Now;
                bool isFindReturnPackage = false;
                int returnIndex = 0;
                int curIndex = 0;
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    curIndex++;
                    string Memo = memInfo.memMsg;
                    if (memInfo.scanType.Contains("退件") || memInfo.scanType.Contains("退回件"))
                    {
                        returnIndex = curIndex;
                        isFindReturnPackage = true;
                        dtReturnDateTime = Convert.ToDateTime(memInfo.scanTime);
                    }
                    if (memInfo.memMsg.Contains("问题件") && isFindReturnPackage && (curIndex - returnIndex) <= 2)
                    {
                        DateTime dtErrorScanTime = Convert.ToDateTime(memInfo.scanTime);
                        TimeSpan ts = dtReturnDateTime - dtErrorScanTime;
                        if (ts.TotalHours > 72)
                        {
                            isReturnPackageDelay = true;
                            break;
                        }
                    }


                }


            }
            catch (Exception ex)
            {

            }
            return isReturnPackageDelay;

        }

        public static bool isReturnPackageDelayed(RoadInfo roadInfo)
        {
            bool isReturnPackageDelay = false;
            try
            {
               
                string strErrorScanTime = roadInfo.errorScanTime;
                DateTime dtReturnDateTime=System.DateTime.Now;
                bool isFindReturnPackage = false;
                int returnIndex = 0;
                int curIndex = 0;
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    curIndex++;
                    string Memo = memInfo.memMsg;
                    if(memInfo.scanType.Contains("退件")|| memInfo.scanType.Contains("退回件"))
                    {
                        returnIndex = curIndex;
                        isFindReturnPackage = true;
                        dtReturnDateTime = Convert.ToDateTime(memInfo.scanTime);
                    }
                    if(memInfo.memMsg.Contains("问题件")&& isFindReturnPackage&&(curIndex- returnIndex)<=2)
                    {
                        DateTime dtErrorScanTime = Convert.ToDateTime(memInfo.scanTime);
                        TimeSpan ts = dtReturnDateTime - dtErrorScanTime;
                        if(ts.TotalHours>72)
                        {
                            isReturnPackageDelay = true;
                            break;
                        }
                    }

                    
                }


            }
            catch (Exception ex)
            {

            }
            return isReturnPackageDelay;

        }

        public static bool isDeliverFailedByWeekend(RoadInfo roadInfo,out bool isDeliverUseWeekend)
        {
            bool isWeekEnd = false;
            isDeliverUseWeekend = false;
            try
            {
                if(roadInfo.errorScanTimes>1)
                {
                    isWeekEnd = true;
                }
                string strErrorScanTime = roadInfo.errorScanTime;
                DateTime dtErrorScanTime = Convert.ToDateTime(strErrorScanTime);
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    string Memo = memInfo.memMsg;

                    if (Utility.isWeekendDeleverPackage(Memo))
                    {
                        isDeliverUseWeekend = true;
                        DayOfWeek dayOffWeek = dtErrorScanTime.DayOfWeek;
                        if(dayOffWeek== DayOfWeek.Saturday|| dayOffWeek== DayOfWeek.Sunday)
                        {
                            isWeekEnd = true;
                            break;
                        }

                    }
                }
            

            }
            catch(Exception ex)
            {

            }
            return isWeekEnd;

        }

        public static bool isWeekendDeleverPackage(string strMemo)
        {
            bool isWeekend = false;
            try
            {
                if (strMemo.Contains("节假日延迟派送") || strMemo.Contains("消费者原因") || strMemo.Contains("延迟送"))
                {
                    isWeekend = true;
                }
            }
            catch(Exception ex)
            {

            }
            return isWeekend;

        }

        public static bool isPackagDelay(RoadInfo roadInfo,out TimeSpan tsTotal)
        {
            bool isPackagDelay = true;
            tsTotal = new TimeSpan();
            try
            {
                if (roadInfo.depMySiteSendTime == "" && roadInfo.lanshouTime != "")
                {
                    roadInfo.depMySiteSendTime = roadInfo.lanshouTime;
                }

                DateTime dtNow = System.DateTime.Now;
                int day = 4;
                if (StoParams.Instance.ht_sto_province_day.ContainsKey(roadInfo.province))
                {
                    RunTimeBean runTimeBean = StoParams.Instance.ht_sto_province_day[roadInfo.province] as RunTimeBean;
                    string strDay = "";
                    if ((roadInfo.lastSiteStatus.Contains("派送") || roadInfo.lastSiteStatus.Contains("签收") || roadInfo.assignPackTime != ""))
                    {
                        strDay = runTimeBean.endDay.ToString();
                    }
                    else
                    {
                        if (roadInfo.yizhanRecTime == "")
                        {
                            strDay = runTimeBean.beginDay.ToString();
                        }
                        else
                        {
                            strDay = runTimeBean.endDay.ToString();
                        }
                    }

                    day = Convert.ToInt32(strDay);
                }
                else
                {
                    if (roadInfo.province.Length > 0)
                    {
                        int a = 0;
                        //swb.WriteLine(roadInfo.province + "不存在");
                    }

                }
                DateTime dtMySiteScanTime = Convert.ToDateTime(roadInfo.depMySiteSendTime);
                DateTime dtEndTime = dtMySiteScanTime.AddDays(day);
                string strEnd = dtEndTime.ToString("yyyy-MM-dd") + " 23:59:59";
                dtEndTime = Convert.ToDateTime(strEnd);
                if (roadInfo.isErrorPackage)
                {
                    DateTime dtErrorScan = Convert.ToDateTime(roadInfo.errorScanTime);
                    if (dtErrorScan > dtEndTime)
                    {
                        isPackagDelay = true;
                        if (roadInfo.isSortWrong)
                        {
                            roadInfo.strErrorMsg = "错发延误";
                            return isPackagDelay;
                        }
                    }
                    if (roadInfo.tomorrowSend)
                    {
                        dtEndTime = dtEndTime.AddDays(1);
                    }
                }


                //先判断到达驿站或者签收的时间是否超时
                if (roadInfo.yizhanRecTime != "" || roadInfo.donePackTime != "")
                {
                    if (roadInfo.yizhanRecTime != "")
                    {
                        if (roadInfo.depMySiteSendTime != "")
                        {

                            if (Convert.ToDateTime(roadInfo.yizhanRecTime) > dtEndTime)
                            {
                                tsTotal = Convert.ToDateTime(roadInfo.yizhanRecTime) - dtEndTime;
                                isPackagDelay = true;
                            }
                            if (isPackagDelay)
                            {
                                roadInfo.strErrorMsg = "延误";
                                if (roadInfo.isSortWrong)
                                {
                                    roadInfo.strErrorMsg = "错发延误";
                                }
                            }
                        }

                    }
                    else if (roadInfo.donePackTime != "")
                    {
                        DateTime dtDonePackTime = Convert.ToDateTime(roadInfo.donePackTime);
                        if (dtDonePackTime > dtEndTime)
                        {
                            tsTotal = dtDonePackTime - dtEndTime;
                            isPackagDelay = true;
                            roadInfo.strErrorMsg = "延误";
                            if (roadInfo.isSortWrong)
                            {
                                roadInfo.strErrorMsg = "错发延误";
                            }
                        }
                    }

                }

                else
                {
                    //只需要判断runtime是否大于指定的天数
                    string strRunTime = roadInfo.runTime;
                    DateTime dtLastScanTime = Convert.ToDateTime(roadInfo.lastSiteScanDate);
                    TimeSpan tsDiff = dtNow - dtLastScanTime;
                    tsTotal = tsDiff;
                    if (dtLastScanTime > dtEndTime || tsDiff.TotalHours > 48)
                    {
                        isPackagDelay = true;
                    }
                    if (isPackagDelay)
                    {
                        if (strRunTime.Contains("天"))
                        {
                            string strCostDay = strRunTime.Substring(0, strRunTime.IndexOf("天"));
                            if (Convert.ToInt32(strCostDay) >= 7)
                            {
                                roadInfo.strErrorMsg = "延误或遗失";
                            }
                            else
                            {
                                roadInfo.strErrorMsg = "延误";
                            }
                        }
                        else
                        {
                            roadInfo.strErrorMsg = "延误";
                        }

                        return isPackagDelay;

                    }

                }
            }
            catch(Exception ex)
            {

            }
            return isPackagDelay;
        }



        public static bool isTimeDealyedByWrongSort(RoadInfo roadInfo)
        {
            bool isDelayed = false;
            try
            {
                
                string strErrorScanTime = roadInfo.errorScanTime;
                DateTime dtErrorScanTime = Convert.ToDateTime(strErrorScanTime);
                string mySiteName=StoParams.Instance.register_site;
                int curIndex = 0;
                string strLastZyToCompanyTime = "";
                string strFirstZyToCompanyTime = "";
                bool isBingoWrongSort = false;
                string strCenterName = "";
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    string Memo = memInfo.memMsg;
                    if(isCurSiteIsZYToCompany(memInfo))
                    {
                        if(strLastZyToCompanyTime == ""&& !isBingoWrongSort)
                        {
                            strLastZyToCompanyTime = memInfo.scanTime;
                            strFirstZyToCompanyTime = memInfo.scanTime;
                            strCenterName = memInfo.uploadSiteName;
                            roadInfo.firstSendToCompany = strFirstZyToCompanyTime;
                            roadInfo.lastCenterName = strCenterName;
                        }
                        else
                        {
                            if(isBingoWrongSort&& strFirstZyToCompanyTime == "")
                            {
                                strFirstZyToCompanyTime = memInfo.scanTime;
                                strCenterName = memInfo.uploadSiteName;
                                roadInfo.firstSendToCompany = strFirstZyToCompanyTime;
                                roadInfo.lastCenterName = strCenterName;
                            }
                        }
                    }            

                    if (Memo.Contains("建包错发件")|| Memo.Contains("网络错分件"))
                    {
                        isBingoWrongSort = true;
                    }
                }

                





            }
            catch (Exception ex)
            {

            }
            return isDelayed;

        }



        public static void DeliverdByTomorrow(RoadInfo roadInfo,out bool isChaoPai,out bool isDoubleTomorrow)
        {
            bool isWeekEnd = false;
            isChaoPai = false;
            isDoubleTomorrow = false;
            try
            {
                if (roadInfo.errorScanTimes > 1)
                {
                    isWeekEnd = true;
                }
                int tomorrowCount = 0;
                string strErrorScanTime = roadInfo.errorScanTime;
                DateTime dtErrorScanTime = Convert.ToDateTime(strErrorScanTime);
                string strFinishTime = "";
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    string Memo = memInfo.memMsg;                   

                    if (Memo.Contains("明日")||Memo.Contains("进行了问题件扫描，扫描类型【延迟送】"))
                    {
                        roadInfo.tomorrowSend = true;
                        tomorrowCount++;
                        return;

                    }
                    if (Memo.Contains("超派件")||Memo.Contains("发件公司要求退回"))
                    {
                        isChaoPai = true;
                        return;
                    }
                    if(tomorrowCount>1)
                    {
                        isDoubleTomorrow = true;
                        return;
                    }
                }


            }
            catch (Exception ex)
            {

            }
            

        }


        public static bool isDeliverMultiOutAndIn(RoadInfo roadInfo)
        {
            bool delayed = false;
            try
            {
                int lastGetIn = 0;
                int firstGetOut = 0;
                int secondGetIn = 0;
                bool findDS = false;
                int curIndex = 0;
                int deliverGetoutCount = 0;
                int deliverGetInCount = 0;
                string strFirstGetInDate = "";
                string strLastGetOutDate = "";
                int getOutIndex = 0;
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    string Memo = memInfo.memMsg;
                
                    if(Memo.Contains("快递员取出"))
                    {
                        getOutIndex = curIndex;
                        deliverGetoutCount++;
                        if (firstGetOut==0)
                        {
                            firstGetOut = curIndex;
                            strLastGetOutDate = memInfo.scanTime;
                        }
                                               
                    }
                    else if(isYiZhanRec(Memo,memInfo))
                    {
                        strFirstGetInDate = memInfo.scanTime;
                        deliverGetInCount++;
                    }
                    curIndex++;
                }
                if(strFirstGetInDate != ""&& strLastGetOutDate != "")
                {
                    DateTime dtFirstGetIn = Convert.ToDateTime(strFirstGetInDate);
                    DateTime dtLastGetOut = Convert.ToDateTime(strLastGetOutDate);
                    if(dtLastGetOut.Date!= dtFirstGetIn.Date&&(getOutIndex>0))
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "快递被快递员取出";
                    }

                }

             
            }
            catch(Exception ex)
            {

            }
            return delayed;
        }

        public static bool isTransferAfterSignNewGen(RoadInfo roadInfo)
        {
            bool delayed = false;
            try
            {
                MemInfo memInfo = new MemInfo();
                string strDoneTime = "";
                string strYizhanTime = "";
                string strAssignTime = "";
                string strArrivalTime = "";
                string strDonePackSite = "";
                bool isPriorRecordIsTomorrowSend = false;
                foreach (string strLine in roadInfo.arListMems)
                {
                    string[] arr = strLine.Split('\t');
                    if (arr.Length < 4)
                    {
                        continue;
                    }
                    if ((arr[1].Trim() == "货样" && !arr[0].Contains(":")) || UtilityHelp.Instance.IsNumeric(arr[0].Trim()) || arr[0].Trim() == "货样")
                    {
                        continue;
                    }
                   
                    if(arr[3].Contains("节假日延迟派送"))
                    {
                        DateTime dtScan = Convert.ToDateTime(arr[0]);
                        DayOfWeek dayOffWeek = dtScan.DayOfWeek;
                        if (dayOffWeek != DayOfWeek.Saturday && dayOffWeek != DayOfWeek.Sunday)
                        {
                            roadInfo.isFakeWeekendDeliver = true;
                        }
                        else
                        {
                            if(dayOffWeek == DayOfWeek.Sunday)
                            {
                                roadInfo.addWeekEndDays = 1;
                            }
                            else if (dayOffWeek == DayOfWeek.Saturday)
                            {
                                roadInfo.addWeekEndDays = 2;
                            }
                        }
                    }
                    string strUploadSite = arr[1];
                    string strScanType = arr[2].ToString();
                    if (strScanType.Contains("签收")&& strDoneTime=="")
                    {
                        strDonePackSite = strUploadSite;
                        strDoneTime = arr[0];
                        continue;
                    }
                    if(strScanType.Contains("派件")&&strDonePackSite== strUploadSite)
                    {
                        DateTime dtDone = Convert.ToDateTime(strDoneTime);
                        DateTime dtAssign = Convert.ToDateTime(arr[0]);

                        if (Convert.ToDateTime(dtAssign.ToShortDateString()) > Convert.ToDateTime(dtDone.ToShortDateString()))
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "签收后又派件";
                            roadInfo.lastSiteName = strUploadSite;
                            return delayed;
                        }

                        
                    }

                    if(isPriorRecordIsTomorrowSend)
                    {
                        if(roadInfo.scanTomorrowSentTime.Length>1)
                        {
                            DateTime dtTomorowSendTime = Convert.ToDateTime(Convert.ToDateTime(roadInfo.scanTomorrowSentTime).ToShortDateString());
                            DateTime dtCurRecord = Convert.ToDateTime( Convert.ToDateTime(arr[0]).ToShortDateString());
                            if(dtCurRecord> dtTomorowSendTime.AddDays(1))
                            {
                                delayed = true;
                                roadInfo.strErrorMsg = "明日送未送";
                                return delayed;
                            }
                        }
                        
                    }
                    if (strScanType.Contains("到件") && strDonePackSite == strUploadSite)
                    {
                        DateTime dtDone = Convert.ToDateTime(strDoneTime);
                        DateTime dtArrival = Convert.ToDateTime(arr[0]);

                        if (Convert.ToDateTime(dtArrival.ToShortDateString()) > Convert.ToDateTime(dtDone.ToShortDateString()))
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "签收后又到件";
                            roadInfo.lastSiteName = strUploadSite;
                            return delayed;
                        }
                    }

                    if (strUploadSite!=roadInfo.DonePackSite)
                    {
                        continue;
                    }
                    
                    if(strScanType.Contains("代收"))
                    {
                        strYizhanTime = arr[0];
                    }

                    if (strScanType.Contains("派件"))
                    {
                        strAssignTime = arr[0];
                    }
                    if (strScanType.Contains("到件")&&strUploadSite==roadInfo.DonePackSite)
                    {
                        strArrivalTime = arr[0];
                        if(roadInfo.lastArrivalSignSiteTime=="")
                        {
                            roadInfo.lastArrivalSignSiteTime = strArrivalTime;
                        }
                    }

                    if(strUploadSite==roadInfo.DonePackSite)
                    {
                        if(arr[3].Contains("话联系不上明日送")||arr[3].Contains("电话联系明日送"))
                        {
                            roadInfo.tomorrowSend = true;
                            roadInfo.scanTomorrowSentTime = arr[0];
                            isPriorRecordIsTomorrowSend = true;
                            continue;
                        }
                        else
                        {
                            isPriorRecordIsTomorrowSend = false;
                        }
                    }
                    if (strDoneTime!=""&&strYizhanTime!="")
                    {
                        DateTime dtDone = Convert.ToDateTime(strDoneTime);
                        DateTime dtYizhanRec = Convert.ToDateTime(strYizhanTime);
                      
                        if (Convert.ToDateTime(dtYizhanRec.ToShortDateString()) > Convert.ToDateTime(dtDone.ToShortDateString()))
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "签收后又入驿站";
                            roadInfo.lastSiteName = strUploadSite;
                        }

                    }

                    if (strDoneTime != "" && strAssignTime != "")
                    {
                        DateTime dtDone = Convert.ToDateTime(strDoneTime);
                        DateTime dtAssign = Convert.ToDateTime(strAssignTime);
                        if (Convert.ToDateTime(dtAssign.ToShortDateString()) > Convert.ToDateTime(dtDone.ToShortDateString()))
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "签收后又派送";
                            roadInfo.lastSiteName = strUploadSite;
                        }

                    }

                    if (strDoneTime != "" && strArrivalTime != "")
                    {
                        DateTime dtDone = Convert.ToDateTime(strDoneTime);
                        DateTime dtArrival = Convert.ToDateTime(strArrivalTime);
                        if (Convert.ToDateTime(dtArrival.ToShortDateString()) > Convert.ToDateTime(dtDone.ToShortDateString()))
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "签收后又到件";
                            roadInfo.lastSiteName = strUploadSite;
                        }
                    }

                    if (strYizhanTime != "" && strArrivalTime != "")
                    {
                        DateTime dtYizhan = Convert.ToDateTime(strYizhanTime);
                        DateTime dtArrival = Convert.ToDateTime(strArrivalTime);
                        if (Convert.ToDateTime(dtArrival.ToShortDateString()) > Convert.ToDateTime(dtYizhan.ToShortDateString()))
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "入驿站后又到件";
                            roadInfo.lastSiteName = strUploadSite;
                        }
                    }
                    if (strYizhanTime != "" && strAssignTime != "")
                    {
                        DateTime dtYizhan = Convert.ToDateTime(strYizhanTime);
                        DateTime dtAssigh = Convert.ToDateTime(strAssignTime);
                        if (Convert.ToDateTime(dtAssigh.ToShortDateString()) > Convert.ToDateTime(dtYizhan.ToShortDateString()))
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "入驿站后派件";
                            roadInfo.lastSiteName = strUploadSite;
                        }
                    }
                    /*DateTime dtDone = Convert.ToDateTime(strDoneTime);
                    DateTime dtYizhanRec = Convert.ToDateTime(strLastYizhanRecDate);
                    if (Convert.ToDateTime(dtYizhanRec.ToShortDateString()) > Convert.ToDateTime(dtDone.ToShortDateString()))
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "签收后又入驿站";
                        roadInfo.lastSiteName = strLastSite;
                    }*/

                }
            }
            catch(Exception ex)
            {

            }
            return delayed;
        }

        public static bool isTransferAfterSign(RoadInfo roadInfo)
        {
            bool delayed = false;
            try
            {
                int lastGetIn = 0;
                int firstGetOut = 0;
                int secondGetIn = 0;
                bool findDS = false;
                int curIndex = 0;
                int deliverGetoutCount = 0;
                int deliverGetInCount = 0;
                string strLastTransferTime = "";
                string strLastSite = "";
                int lastYizhanRec = 0;
                string strLastYizhanRecDate = "";
                string strLastAssingPack = "";
                string strFirstCompany = "";

                SiteActRecords siteActRecords = new bean.SiteActRecords();
                foreach (MemInfo memInfo in roadInfo.arListMems)
                {
                    string Memo = memInfo.memMsg;
                    string strPriNext = memInfo.preOrNext;
                    string strUploadSite = memInfo.uploadSiteName;
            
                    if (memInfo.uploadSiteName==roadInfo.lastSiteName)
                    {
                        if ((memInfo.scanType == "派送中" || memInfo.scanType == "派件中" || Memo.Contains("正在派件")|| Memo.EndsWith("-派件中") || Memo.Contains("正在派送途中"))&&!Memo.Contains("已签收"))
                        {
                            siteActRecords.arrsignList.Add(Convert.ToDateTime(memInfo.scanTime).ToShortDateString());
                        }

                        if ((memInfo.scanType == "运输中" || Memo.Contains("已到达"))&& !Memo.Contains("已签收"))
                        {
                            siteActRecords.arrivalList.Add(Convert.ToDateTime(memInfo.scanTime).ToShortDateString());
                        }
                        if(Memo.Contains("已签收"))
                        {
                            siteActRecords.doneList.Add(Convert.ToDateTime(memInfo.scanTime).ToShortDateString());
                        }

                    }
             
                    if (strPriNext==""&&Memo.Contains("发往"))
                    {
                        if(Memo.Contains("-"))
                        {
                            
                            strPriNext = Memo.Substring(Memo.LastIndexOf("-")+1);
                            strPriNext = strPriNext.Replace("【", "");
                            strPriNext = strPriNext.Replace("】", "");
                        }
                        else if(Memo.Contains("【"))
                        {
                            strPriNext = Memo.Substring(Memo.LastIndexOf("【") + 1, Memo.LastIndexOf("】") - Memo.LastIndexOf("【") - 1);
                            strPriNext = strPriNext.Replace("【", "");
                            strPriNext = strPriNext.Replace("】", "");
                        }
                        
                        

                    }
                    string strKey = strUploadSite + "-" + strPriNext;
                    if(MemberInfo.ht_NextDay_Company.ContainsKey(strKey))
                    {
                        string strSplitTime = MemberInfo.ht_NextDay_Company[strKey].ToString();
                        string strScanTime = memInfo.scanTime;
                        strSplitTime = strScanTime.Substring(0, strScanTime.IndexOf(" ")) + " " + strSplitTime;
                        if(Convert.ToDateTime(strSplitTime)<Convert.ToDateTime(strScanTime))
                        {
                            roadInfo.isNextDayArrival = true;
                        }
                    }

                    if(MemberInfo.ht_Far_Company.ContainsKey(strKey))
                    {
                        FarCompany farCompany = MemberInfo.ht_Far_Company[strKey] as FarCompany;
                        int farDays = farCompany.addDays;
                        roadInfo.isFarCompany = true;
                        roadInfo.farCompanyDelay = farDays;
                        roadInfo.farCompanyScanTime = memInfo.scanTime;

                    }
                    if (isYiZhanRec(Memo, memInfo)&& curIndex< roadInfo.arListMems.Count/2)
                    {
                        if(Memo.Contains("代收，请及时取件")&& !Memo.Contains("驿站"))
                        {
                            continue;
                        }
                        lastYizhanRec = curIndex;
                        deliverGetInCount++;
                        if(strLastYizhanRecDate=="")
                        {
                            strLastYizhanRecDate = memInfo.scanTime;
                        }                                              
                    }
                    if((memInfo.scanType.Contains("派送中") && strLastAssingPack == ""&&
                        (memInfo.memMsg.EndsWith("派件中")|| memInfo.memMsg.EndsWith("派送中")||memInfo.memMsg.Contains("正在派送途中")))&&!memInfo.memMsg.Contains("已签收"))
                    {
                        strLastAssingPack = memInfo.scanTime;
                        strLastSite = memInfo.uploadSiteName;
                    }
                    if (strFirstCompany == "" && curIndex < roadInfo.arListMems.Count)
                    {
                        if (isCurSiteIsZYToCompany(memInfo))
                        {
                            roadInfo.lastCompanyName = strPriNext;
                            strFirstCompany = strPriNext;
                            roadInfo.lastCenterName = strUploadSite;
                            roadInfo.firstSendToCompany = memInfo.scanTime;

                        }//if (isCurSiteIsZYToCompany(memInfo))
                    }

                    curIndex++;
                }
                string strDoneTime = "";
                
                if (roadInfo.donePackTime != "")
                {
                    strDoneTime = roadInfo.donePackTime;
                }
                /*if (strLastTransferTime != ""&& strDoneTime!="")
                {
                    DateTime dtDone = Convert.ToDateTime(strDoneTime);
                    DateTime dtLastTransfer = Convert.ToDateTime(strLastTransferTime);
                    if (dtDone < dtLastTransfer)
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "签收后又运输延迟";
                        roadInfo.lastSiteName = strLastSite;
                    }

                }*/
                if(strDoneTime != ""&& strLastYizhanRecDate!="")
                {
                    DateTime dtDone = Convert.ToDateTime(strDoneTime);
                    DateTime dtYizhanRec = Convert.ToDateTime(strLastYizhanRecDate);
                    if(Convert.ToDateTime(dtYizhanRec.ToShortDateString())> Convert.ToDateTime(dtDone.ToShortDateString()))
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "签收后又入驿站";
                        roadInfo.lastSiteName = strLastSite;
                    }
                }

                if(strLastAssingPack!=""&&roadInfo.yizhanRecTime!="")
                {
                    DateTime dtLastAssign = Convert.ToDateTime(strLastAssingPack);
                    DateTime dtFirstYiZhan = Convert.ToDateTime(roadInfo.yizhanRecTime);
                    if (Convert.ToDateTime(dtLastAssign.ToShortDateString()) > Convert.ToDateTime(dtFirstYiZhan.ToShortDateString()))
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "入驿站后又派送";
                        roadInfo.lastSiteName = strLastSite;
                    }
                }

                if (strLastAssingPack != "" && roadInfo.donePackTime != ""&& !delayed)
                {
                    DateTime dtLastAssign = Convert.ToDateTime(strLastAssingPack);
                    DateTime dtDone = Convert.ToDateTime(roadInfo.donePackTime);
                    if (Convert.ToDateTime(dtLastAssign.ToShortDateString()) > Convert.ToDateTime(dtDone.ToShortDateString()))
                    {
                        delayed = true;
                        roadInfo.strErrorMsg = "签收后又派送";
                        roadInfo.lastSiteName = strLastSite;
                    }
                }
                
                if(!delayed && roadInfo.donePackTime != "")
                {
                    DateTime dtFirstDone = Convert.ToDateTime(roadInfo.donePackTime.Substring(0, roadInfo.donePackTime.IndexOf(' ')));
                    foreach(string strArrival in siteActRecords.arrivalList)
                    {
                        DateTime dtArrival = Convert.ToDateTime(strArrival);
                        if (dtArrival> dtFirstDone)
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "签收后又到件";
                        }
                    }

                    foreach (string  strAssing in siteActRecords.arrsignList)
                    {
                        DateTime dtAssing = Convert.ToDateTime(strAssing);
                        if (dtAssing > dtFirstDone)
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "签收后派件";
                        }
                    }
                }
                if (roadInfo.donePackTime == ""&& roadInfo.yizhanRecTime!=""&&!delayed)
                {
                    DateTime dtFirstYizhan = Convert.ToDateTime(roadInfo.yizhanRecTime.Substring(0, roadInfo.yizhanRecTime.IndexOf(' ')));
                    foreach (string  strArrival in siteActRecords.arrivalList)
                    {
                        DateTime dtArrival = Convert.ToDateTime(strArrival);
                        if (dtArrival > dtFirstYizhan)
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "入驿站后又到件";
                        }
                    }

                    foreach (string strAssing in siteActRecords.arrsignList)
                    {
                        DateTime dtAssing = Convert.ToDateTime(strAssing);
                        if (dtAssing > dtFirstYizhan)
                        {
                            delayed = true;
                            roadInfo.strErrorMsg = "入驿站后派件";
                        }
                    }
                }


            }
            catch (Exception ex)
            {

            }
            return delayed;
        }


        public static string getShouldEndDateTime(DateTime dtCenterToCompany,string strCenterName,string strCompanyName,RoadInfo roadInfo)
        {
            string strRes = "";
            try
            {
                string strCenterTime = "";
                if (!dtCenterToCompany.ToString().StartsWith("2000") && strCenterName != "")
                {
                    if(roadInfo.isNextDayArrival)
                    {
                        DateTime dtDead = Convert.ToDateTime(dtCenterToCompany).AddDays(1);
                        strRes = dtDead.ToString();
                        return strRes;
                    }
                    if(roadInfo.isFarCompany)
                    {
                        DateTime dtDead = Convert.ToDateTime(roadInfo.farCompanyScanTime).AddDays(roadInfo.farCompanyDelay);
                        strRes = dtDead.ToString();
                        return strRes;

                    }

                    string leaveCenterTime = dtCenterToCompany.ToShortTimeString();
                    string deadTime = "10:00";
                    if (MemberInfo.ht_Each_Center_Time.ContainsKey(strCenterName))
                    {
                        deadTime = MemberInfo.ht_Each_Center_Time[strCenterName] as string;
                    }
                    deadTime = dtCenterToCompany.ToShortDateString() + " " + deadTime;
                    bool add = false;
                    if (dtCenterToCompany > Convert.ToDateTime(deadTime))
                    {
                        add = true;
                        DateTime dtDead = Convert.ToDateTime(dtCenterToCompany).AddDays(1);
                        strRes = dtDead.ToString();
                    }
                    else
                    {
                        DateTime dtDead = Convert.ToDateTime(dtCenterToCompany).AddDays(0);
                        strRes = dtDead.ToString();
                    }
                    if(!add)
                    {
                        if(strCompanyName!=""&&MemberInfo.ht_Far_Company.ContainsKey(strCompanyName))
                        {
                            DateTime dtDead = Convert.ToDateTime(dtCenterToCompany).AddDays(1);
                            strRes = dtDead.ToString();
                        }
                    }
                    
                }
            }
            catch(Exception ex)
            {

            }
            return strRes;
            
        }


        public static bool isRefreshByMysiteNewGen(RoadInfo roadInfo)
        {
            bool isRefresh = false;
            try
            {
                string refreshSite = "";
                int diffCount = 0;
                
                foreach (string strLine in roadInfo.arListMems)
                {
                    string[] arr = strLine.Split('\t');
                    if (arr.Length < 4)
                    {
                        continue;
                    }
                    if ((arr[1].Trim() == "货样" && !arr[0].Contains(":")) || UtilityHelp.Instance.IsNumeric(arr[0].Trim()) || arr[0].Trim() == "货样")
                    {
                        continue;
                    }
                    string strUploadSite = arr[1];
                    string strType = arr[2];
                    if (strUploadSite == StoParams.Instance.register_site)
                    {
                        continue;
                    }
                    else
                    {
                        if(strUploadSite.Contains("转运中心")|| strUploadSite.Contains("集散中心") || strUploadSite.Contains("分拨") || strUploadSite.Contains("航空部"))
                        {
                            if(strType.Contains("发件"))
                            {
                                diffCount++;
                            }
                        }
                       
                    }
                }
                    

                if (diffCount < 1)
                {
                    isRefresh = true;
                }

            }
            catch (Exception ex)
            {

            }
            return isRefresh;

        }


        public static bool isRefreshByMysite(RoadInfo roadInfo)
        {
            bool isRefresh = false;
            try
            {
                string refreshSite = "";
                int diffCount = 0;
                foreach(MemInfo memInfo in roadInfo.arListMems)
                {
                    if(memInfo.uploadSiteName==StoParams.Instance.register_site)
                    {
                        continue;
                    }
                    else
                    {
                        diffCount++;
                    }
                }
                if(diffCount<1)
                {
                    isRefresh = true;
                }

            }
            catch(Exception ex)
            {

            }
            return isRefresh;

        }


        public static void addBarcodeToFile(ArrayList arListBarcodes,int beginIndex,int endIndex,string strFileDir)
        {
            try
            {
                DeleteDir(strFileDir);
                string fileName = strFileDir + "\\" + beginIndex.ToString() + "_" + endIndex.ToString() + ".txt";
                FileStream bFile = new FileStream(fileName, FileMode.Create);
                StreamWriter swb = null;
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                }
                
                foreach(string strLine in arListBarcodes)
                {
                    swb.WriteLine(strLine);
                }
                swb.Close();
            }
            catch(Exception ex)
            {

            }
        }
        public static void DeleteDir(string file)
        {
            try
            {
                //去除文件夹和子文件的只读属性
                //去除文件夹的只读属性
                System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
                fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;

                //去除文件的只读属性
                System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);

                //判断文件夹是否还存在
                if (Directory.Exists(file))
                {
                    foreach (string f in Directory.GetFileSystemEntries(file))
                    {
                        if (File.Exists(f))
                        {
                            //如果有子文件删除文件
                            File.Delete(f);
                            Console.WriteLine(f);
                        }
                        else
                        {
                            //循环递归删除子文件夹
                            DeleteDir(f);
                        }
                    }

                    //删除空文件夹
                    //Directory.Delete(file);
                    Console.WriteLine(file);
                }

            }
            catch (Exception ex) // 异常处理
            {
                Console.WriteLine(ex.Message.ToString());// 异常信息
            }
        }

        private static void handleActConculTime(RoadInfo roadInfo)
        {
            try
            {
                string lastZyCenter = "";
                foreach(string strLine in roadInfo.arListMems)
                {
                    string[] arr = strLine.Split('\t');
                    if (arr.Length < 4)
                    {
                        continue;
                    }

                    if ((arr[1].Trim() == "货样" && !arr[0].Contains(":")) || UtilityHelp.Instance.IsNumeric(arr[0].Trim()) || arr[0].Trim() == "货样")
                    {
                        continue;
                    }
                    DateTime dtOpTime = Convert.ToDateTime(arr[0].Trim());

                    if (arr[1].Contains("集散中心") || arr[1].Contains("转运中心") || arr[1].Contains("分拨") || arr[1].Contains("航空部"))
                    {
                        lastZyCenter= arr[1];
                        continue;
                    }                   
                }

                bool bingo = false;
                string strLastZyCenterTime = "";
                foreach (string strLine in roadInfo.arListMems)
                {
                    string[] arr = strLine.Split('\t');
                    if (arr.Length < 4)
                    {
                        continue;
                    }

                    if ((arr[1].Trim() == "货样" && !arr[0].Contains(":")) || UtilityHelp.Instance.IsNumeric(arr[0].Trim()) || arr[0].Trim() == "货样")
                    {
                        continue;
                    }
                    DateTime dtOpTime = Convert.ToDateTime(arr[0].Trim());
                    if(arr[1]==lastZyCenter)
                    {
                        bingo = true;
                    }
                    if(bingo)
                    {
                        if (arr[1].Contains("集散中心") || arr[1].Contains("转运中心") || arr[1].Contains("航空部"))
                        {
                            strLastZyCenterTime = dtOpTime.ToString();
                            continue;
                        }
                         if (arr[1]!= lastZyCenter)
                        {
                            string strKey = lastZyCenter + "-" + arr[1];
                            if (StoParams.Instance.htSiteTimeMap.ContainsKey(strKey))
                            {
                                TimeBean timeBean = StoParams.Instance.htSiteTimeMap[strKey] as TimeBean;

                                int nAddDays = 0;
                                try
                                {
                                    nAddDays = Convert.ToInt32(timeBean.addDays);
                                    DateTime dtSendTime = Convert.ToDateTime(strLastZyCenterTime);
                                    DateTime dtSendTimeLast = Convert.ToDateTime(dtSendTime.ToShortDateString() + " " + "23:59:59");
                                    dtSendTimeLast = dtSendTimeLast.AddDays(nAddDays);
                                    DateTime dtDanPaiTime = Convert.ToDateTime(dtSendTime.ToShortDateString() + " " + timeBean.firstAssignTime);
                                    if (dtSendTime < dtDanPaiTime)
                                    {
                                        roadInfo.actConculTime = dtSendTimeLast.ToString();
                                    }
                                    else
                                    {
                                        roadInfo.actConculTime = dtSendTimeLast.AddDays(1).ToString();
                                    }
                                    return;
                                }
                                catch (Exception ex)
                                {

                                }
                            }

                        }
                    }
                }
            }
            catch(Exception ex)
            {

            }
        }


        public static void getSepTime(string time,out int hour,out int minute)
        {
            hour = 0;
            minute = 0;
            try
            {
                string[] arr = time.Split(':');
                hour = Convert.ToInt32(arr[0]);
                minute = Convert.ToInt32(arr[1]);
            }
            catch(Exception ex)
            {

            }
        }

        public static bool isDelayMore(string priorDelay,string currentDelay)
        {
            bool res = false;
            try
            {
                if(priorDelay=="")
                {
                    res = true;
                }
                string priorDay = priorDelay.Substring(0, priorDelay.IndexOf("天"));
                string priorHour = priorDelay.Substring(priorDelay.IndexOf("天")+1, priorDelay.IndexOf("小时")- priorDelay.IndexOf("天")-1);

                string curDay = currentDelay.Substring(0, currentDelay.IndexOf("天"));
                string curHour = currentDelay.Substring(currentDelay.IndexOf("天") + 1, currentDelay.IndexOf("小时") - currentDelay.IndexOf("天") - 1);
                TimeSpan tsPrior = new TimeSpan(Convert.ToInt32(priorDay), Convert.ToInt32(priorHour), 0, 0);
                TimeSpan tsCurr = new TimeSpan(Convert.ToInt32(curDay), Convert.ToInt32(curHour), 0, 0);
                if(tsCurr>tsPrior)
                {
                    res = true;
                }


            }
            catch(Exception ex)
            {

            }
            return res;
        }

        public static bool IsNullOrEmpty(JToken token)
        {
            return (token == null) ||
                   (token.Type == JTokenType.Array && !token.HasValues) ||
                   (token.Type == JTokenType.Object && !token.HasValues) ||
                   (token.Type == JTokenType.String && token.ToString() == String.Empty) ||
                   (token.Type == JTokenType.Null);
        }

        public static  bool isSorWrongThisSite(int priorSiteIndex,int curSiteIndex, JArray details,string curSite,out string errorSendSite)
        {
            bool sortWrong = false;
            errorSendSite = "";
            try
            {
                for (int i = priorSiteIndex; i >= curSiteIndex; i--)
                {
                    JObject jason = (JObject)details[i];                   
                    JToken scanNetworkName = jason["scanNetworkName"];
                    if(!IsNullOrEmpty(scanNetworkName))
                    {
                        if(scanNetworkName.ToString()!= curSite)
                        {
                            sortWrong = true;
                            errorSendSite = scanNetworkName.ToString();
                            break;
                        }
                    }
                }
            }
            catch(Exception ex)
            {

            }
            return sortWrong;

        }


        public static bool isTimeRight(DateTime dt)
        {
            bool res = false;
            try
            {
                if(dt.ToString().StartsWith("20"))
                {
                    res = true;
                }
            }
            catch(Exception ex)
            {

            }
            return res;
        }


        public  static TimeSpan getDelayTime(ArrayList arListRoads,string errorDepSite,string sortedWongSite, DateTime dtErrorBegin)
        {
            TimeSpan ts=new TimeSpan(0,0,0);
            try
            {
                bool findNextRight = false;
                foreach(RoadInfo road in arListRoads)
                {
                    if(road.jtSiteName== sortedWongSite && road.jtNextStopName== errorDepSite)
                    {
                        findNextRight = true;
                        continue;
                    }
                    if(findNextRight)
                    {
                        if(road.jtSiteName!= errorDepSite)
                        {
                            ts = road.jtScanTime - dtErrorBegin;
                            break;
                        }
                    }
                }
            }
            catch(Exception ex)
            {

            }
            return ts;


        }


        public static int getDelayDays(DateTime dtPlan,DateTime dtAct)
        {
            int res = 0;
            try
            {
                if(dtAct> dtPlan)
                {
                    TimeSpan ts = dtAct - dtPlan;
                    res = Convert.ToInt32(Math.Ceiling(ts.TotalDays));
                }

            }
            catch(Exception ex)
            {

            }
            return res;
        }


        public static void parseBarcodes(ArrayList arListBarcodes,JObject jsonData)
        {
            try
            {
                JArray records = (JArray)jsonData["records"];
                for(int i=0;i< records.Count;i++)
                {
                    JObject curObj = (JObject)records[i];
                    JToken curState = curObj["currentScantType"];
                    if(curState!=null&& curState.ToString().Contains("快件签收"))
                    {
                        continue;
                    }
                    JToken scanTime = curObj["scanTime"];
                    if(scanTime!=null)
                    {

                        StoParams.Instance.lastBarcodeScanTime = scanTime.ToString();
                        DateTime dtScanTime = Convert.ToDateTime(scanTime.ToString());
                        TimeSpan tsCur = DateTime.Now - dtScanTime;
                        if(tsCur.TotalHours<24)
                        {
                            continue;
                        }
                    }
                    JToken barcode = curObj["billCode"];
                    if(barcode!=null)
                    {
                        //arListBarcodes.Add("JT5105792733172");
                        arListBarcodes.Add(barcode.ToString());                        
                    }

                }

            }
            catch(Exception ex)
            {

            }
        }


        public static bool isTaskDone(ArrayList arListRoadMap,string scanType)
        {
            bool res = false;
            try
            {
               
                foreach (RoadBean roadBean in arListRoadMap)
                {
                    if(scanType=="拦截件")
                    {
                        if (roadBean.scanTypeName == "拦截件登记")
                        {
                            res = true;
                        }
                    }
                    if (scanType == "改地址")
                    {
                        if (roadBean.scanTypeName == "转寄登记")
                        {
                            res = true;
                        }
                    }

                }
            }
            catch(Exception ex)
            {

            }
            return res;
        }


        public static void getDestoryType(string addr,DestroyTypeBean bean,string transferType)
        {
            string destroyType = "派件破损";
            string code = "0201";
            try
            {
                if(addr.Contains("破损"))
                {
                    if(transferType.Contains("中转"))
                    {
                        destroyType = "中转破损";
                        code = "0202";
                    }
                }
                if (addr.Contains("短少"))
                {
                    destroyType = "内件短少";
                    code = "0203";
                }
                else if (addr.Contains("污染"))
                {
                    destroyType = "内件污染";
                    code = "0204";
                }
                else if (addr.Contains("不符"))
                {
                    destroyType = "内件不符";
                    code = "0205";
                }
                bean.name = destroyType;
                bean.code = code;

            }
            catch (Exception ex)
            {

            }  

        }

        public static string getDestoryType(string addr)
        {
            string destroyType = "破损";
            try
            {
                if(addr.Contains("短少"))
                {
                    destroyType = "短少";
                }
                else if (addr.Contains("少件"))
                {
                    destroyType = "少件";
                }
                else if (addr.Contains("污染"))
                {
                    destroyType = "污染";
                }

            }
            catch(Exception ex)
            {

            }
            return destroyType;

        }


        public static bool isMySiteParcel(ArrayList arListRoadMap)
        {
            bool res = true;
            try
            {
                foreach (RoadBean roadBean in arListRoadMap)
                {
                    if (roadBean.scanTypeName.Contains("揽收") || roadBean.scanTypeName.Contains("入仓"))
                    {
                        string pickName = roadBean.scanNetworkName;
                        if (!StoParams.Instance.htRegSiteNameAndToken.ContainsKey(pickName) && pickName.Contains("网点"))
                        {
                            return false;
                        }
                    }

                }
            }
            catch(Exception ex)
            {

            }
            return res;
        }

        public static bool isOnlyPackRoadmap(ArrayList arListRoadMap)
        {
            bool res = false;
            try
            {
                
                
                if (arListRoadMap.Count>1)
                {
                    return res;
                }

                foreach (RoadBean roadBean in arListRoadMap)
                {
                    if(roadBean.scanTypeName== "建包扫描")
                    {
                        res = true;
                    }

                }
            }
            catch (Exception ex)
            {

            }
            return res;
        }



        public static  bool canLeaveMsgToday(string barcode)
        {
            bool res = false;
            try
            {
                string todayKey = System.DateTime.Now.ToShortDateString();
                if(!StoParams.Instance.htTodayLeaveMsgCount.ContainsKey(todayKey))
                {                    
                    StoParams.Instance.htTodayLeaveMsgCount[todayKey] = new Hashtable();
                }
                Hashtable htToday = StoParams.Instance.htTodayLeaveMsgCount[todayKey] as Hashtable;
                if(!htToday.ContainsKey(barcode))
                {
                    htToday[barcode] = 1;
                    res = true;
                    return res;
                }
                int totalCount = Convert.ToInt32(htToday[barcode]);
                if(totalCount>10)
                {
                    return res;
                }
                totalCount++;
                htToday[barcode] = totalCount;
                res = true;
            }
            catch(Exception ex)
            {

            }
            return res;

        }








    }
}
